package com.cdut.cn.boke.controller;

import com.cdut.cn.boke.pojo.Blog;
import com.cdut.cn.boke.pojo.User;
import com.cdut.cn.boke.service.BlogService;
import com.cdut.cn.boke.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    /**
     * 用于显示管理员首页
     * */
    @GetMapping
    public String index(Model model) {
        List<Blog> blogList = blogService.selectBlog();
        model.addAttribute("blogList", blogList);
        return "admin/index";
    }

    /**
     * 用于显示登录页面
     * */
    @GetMapping("login")
    public String login() {
        return "admin/login";
    }

    /**
     * 用于处理登录请求
     * */
    @PostMapping("doLogin")
    public String doLogin(Model model, HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password) {
        System.out.println(username);
        System.out.println(password);
        User user = userService.selectUserByName(username);
        System.out.println(user);
        if (user == null) {
            model.addAttribute("msg", "用户名不存在！");
            return "/admin/login";
        } else if (user.getUsername().equals(username) && user.getPassword().equals(password)) { // 登录成功：将用户名存入会话中，跳转管理员首页controller
            session.setAttribute("username", username);
            System.out.println("登录成功，查询数据后跳转管理员首页");
            List<Blog> blogList = blogService.selectBlog();
            model.addAttribute("blogList", blogList);
            return "/admin/index";
        } else { // 登录失败，带上错误信息返回登录页
            model.addAttribute("msg", "用户名或密码错误！");
            return "/admin/login";
        }
    }


    /*
     * 用于显示添加博客页面
     * */
    @GetMapping("/add")
    public String addBlog() {
        return "/admin/add";
    }

    /*
     * 用于处理添加博客页面提交的表单数据
     * */
    @PostMapping("/doAdd")
    public String doAddBlog(Model model, @ModelAttribute Blog blog) {
        LocalDateTime currentTime = LocalDateTime.now();
        Timestamp createDate = new Timestamp(currentTime.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        blog.setCreateDate(createDate);
        int i = blogService.addBlog(blog);
        if (i>0){
            List<Blog> blogList = blogService.selectBlog();
            model.addAttribute("blogList", blogList);
            return "redirect:/admin";
        }else {
            model.addAttribute("msg", "添加失败");
            return "/admin/add";
        }
    }
}

